package com.lysj.merchant.merchant.service;

import cn.hutool.crypto.digest.BCrypt;
import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.spring.foundation.jpa.ConditionUtil;
import com.lysj.admin.utils.spring.foundation.pagination.PageUtil;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.merchant.management.domain.Role;
import com.lysj.merchant.management.domain.User;
import com.lysj.merchant.management.repository.UserRepository;
import com.lysj.merchant.management.service.RoleService;
import com.lysj.merchant.merchant.domain.Merchant;
import com.lysj.merchant.merchant.domain.Store;
import com.lysj.merchant.merchant.dto.MerchantUser;
import com.lysj.merchant.merchant.repository.MerchantRepository;
import com.lysj.merchant.merchant.repository.StoreRepository;
import com.lysj.merchant.merchant.vo.StoreUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author Created by wtl on 2019-03-12 17:25:12
 * @description 商户服务层
 */
@Slf4j
@Service
@Transactional
public class MerchantService implements BaseService<Merchant> {

    @Resource
    private MerchantRepository merchantRepository;

    @Resource
    private StoreRepository storeRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private RoleService roleService;

    @Override
    public MerchantRepository getRepository() {
        return merchantRepository;
    }

    /**
     * @author Created by wtl on 2019/3/15 17:41
     * @Description 后台查看商户列表
     */
    public Page<Merchant> list(Merchant entity, PageVo pageVo) {
        Pageable pageable = PageUtil.initPage(pageVo);
        Specification specification = ConditionUtil.createSpecification(entity);
        Page<Merchant> page = merchantRepository.findAll(specification, pageable);
        List<Merchant> merchants = page.getContent();
        for (Merchant merchant : merchants) {
            User user = userRepository.findByMerchantIdAndUserType(merchant.getId(), 1);
            merchant.setAccount(user.getUsername());
        }
        return page;
    }

    /**
     * @author Created by wtl on 2019/3/15 15:52
     * @Description 总后台创建商户
     */
    public String save(MerchantUser entity) {
        // 创建商户
        Merchant merchant = new Merchant();
        merchant.setBusinessLicense(entity.getBusinessLicense());
        merchant.setLegalPerson(entity.getLegalPerson());
        merchant.setName(entity.getName());
        merchantRepository.save(merchant);
        // 创建门店
        Store store = new Store();
        store.setMerchantId(merchant.getId());
        store.setName("默认门店");
        storeRepository.save(store);
        // 创建角色“店长”
        Role role = new Role();
        role.setName("店长");
        role.setRoleType(Role.RoleType.MANAGER.getCode());
        role.setStoreId(store.getId());
        roleService.save(role);
        // 创建管理员账号
        User user= userRepository.findByUsername(entity.getAdminAccount());
        if (!ParamUtil.isBlank(user)) {
            throw new BaseException("账号已存在",Resp.Status.PARAM_ERROR.getCode());
        }
        User adminUser = new User();
        adminUser.setMerchantId(merchant.getId());
        adminUser.setUsername(entity.getAdminAccount());
        adminUser.setPassword(BCrypt.hashpw("123456"));
        adminUser.setUserType(1); // 商户
        userRepository.save(adminUser);
        return "添加成功,登录密码为123456";
    }

    /**
     * 根据商户id查看对应的门店列表
     */
    public List<StoreUser> findStoreInfo(String merchantId) {


        return storeRepository.findStoreInfo(merchantId);
    }

}